package com.globalresources.business.impl;

import java.util.ArrayList;
import java.util.List;

import com.globalresources.business.inface.IDocumentService;
import com.globalresources.commons.PDFOperate;
import com.globalresources.commons.SignUtil;
import com.globalresources.commons.vo.UserAndDocumentWrapper;
import com.globalresources.dao.inface.IDocumentDao;
import com.globalresources.po.Document;
import com.globalresources.ui.struts.action.document.vo.PatientDocument;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Jul 19, 2009 10:33:50 AM
 */
public class DocumentServiceImpl implements IDocumentService {
	private IDocumentDao documentDao;
	private String adssServer;
	
	
	public IDocumentDao getDocumentDao() {
		return documentDao;
	}

	public void setDocumentDao(IDocumentDao documentDao) {
		this.documentDao = documentDao;
	}
	
	
	
	/**
	 * get documents
	*@author aaron.lu(luzhiyuan)
	*@param userid <cod>Integer</code> 
	*@param isSign <code>Byte</code>
	*@param isView <code>Byte</code>
	*@return
	 */
	public List<Document> getDocuments(Integer userid, Byte isSign, Byte isView){
		
		return documentDao.getDocuments(userid, isSign, isView);
	}
	/**
	 * get documents by Page Number
	*@author songduanlei@gmail.com
	*@param userid <code>Integer</code>
	*@param isSign <code>Byte</code>
	*@param isView <code>Byte</code>
	*@param pageSize<code>int</code>
	*@return
	 */
	public List<Document> getDocumentsByPager(Integer userid, Byte isSign, Byte isView,int currentPage,int pageSize){
		return documentDao.getDocumentsByPager(userid, isSign, isView,currentPage,pageSize);
	}
	/**
	 * count documents
	*@author aaron.lu(luzhiyuan)
	*@param userid
	*@param isSign
	*@param isView
	*@return
	 */
	public int countDocuments(Integer userid, Byte isSign, Byte isView){
		return documentDao.countDocuments(userid, isSign, isView);
	}
	
	/**
	 * load a document
	*@author aaron.lu(luzhiyuan)
	*@param id
	*@return
	 */
	public Document getDocument(Integer id){
		return documentDao.find(id);
	}
	
	/**
	 * save a document
	*@author aaron.lu(luzhiyuan)
	*@param document
	 */
	public void saveDocument(Document document){
		documentDao.save(document);
	}
	
	/**
	 * update a documnet
	*@author aaron.lu(luzhiyuan)
	*@param document
	 */
	public void updateDocument(Document document){
		documentDao.saveOrUpdate(document);
	}
	
	/**
	 * <p>查询所有医院里的文档
	*@author aaron.lu(luzhiyuan)
	*@param hospital_id
	*@return
	 */
	public List<Document> getAllDocumentInHospital(Integer hospital_id){
		return documentDao.getAllDocumentInHospital(hospital_id);		
	}
	
	/**
	 * <p>查询文档统计报表
	*@author aaron.lu(luzhiyuan)
	*@param hospital_id
	*@param order_flag
	*@return
	 */
	public List<UserAndDocumentWrapper> getDocumentSum(Integer hospital_id, String order_flag){
		List<UserAndDocumentWrapper> result = new ArrayList<UserAndDocumentWrapper>();
		if (null != hospital_id) {
			result = documentDao.getDocuemntSum(hospital_id,order_flag);
		}
		return result;
	}

	public int countHospitalDocuments(Integer hospitalid, Byte isSign,
			Byte isView) {
		// TODO Auto-generated method stub
		return this.documentDao.countHospitalDocuments(hospitalid, isSign, isView);
	}

	public List<Document> getHospitalDocuments(Integer hospitalid, Byte isSign,
			Byte isView) {
		// TODO Auto-generated method stub
		return this.documentDao.getHospitalDocuments(hospitalid, isSign, isView);
	}
	
	/**
	 * 返回页码
	 * @param List<Document> documents
	 * @param Document document
	 * */
	
	public int getPageNumber(List<Document> documents,Document document){
		int j=0;
	//	System.out.println("document id"+document.getId());
		for(int i=0;i<documents.size();i++)
		{
			//System.out.println(documents.get(i).getId());
			
			if(Integer.parseInt((documents.get(i).getId().toString()))==document.getId()){
				System.out.println("j="+j);
				return ++j;	
			}
			j++;
		}
		if(j==0){	
			return -1;
		}else{
		return  ++j;
		}
	}
	
	/**
	 * 返回hashmap列表的 文档信息
	 * 
	 * */
	public List<Document> getDocumentEditList(String[] documentCheckBox){
		String id;
		String[] tempArray;
		List<Document> ldocument=new ArrayList<Document>();
		Document document;		
		for(int i=0;i<documentCheckBox.length;i++){			
			id=documentCheckBox[i];
			System.out.println("***********"+id);
			document=documentDao.find(Integer.parseInt(id));
			ldocument.add(document);		
		}		
		return ldocument;
	}
	
	
	/**
	 * 实施签名
	 * 返回boolean
	 * **/
	public boolean doSignDocument(String path,String signReason,String signLocation){
		try{
		int pageNumber=PDFOperate.getPdfPageCount(path);
		String profileNumber=SignUtil.getSignProfile(pageNumber);
		return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}

	/**
	 * @return the adssServer
	 */
	public String getAdssServer() {
		return adssServer;
	}

	/**
	 * @param adssServer the adssServer to set
	 */
	public void setAdssServer(String adssServer) {
		this.adssServer = adssServer;
	}

	public List<Document> getDocumentByPatient(String caseNumber, int currentPage, int pageSize) {
		List<Document> documents = documentDao.getDocumentByPatient(caseNumber, currentPage, pageSize);
		return documents ;
	}

	public int getDocumentNumByPatient(String caseNumber) {
		int number = documentDao.getDocumentNumByPatient(caseNumber);
		return number ;
	}

	public List<Document> getSignDocumentByPatient(String caseNumber, byte isSign, int currentPage, int pageSize) {
		List<Document> documents = documentDao.getSignDocumentByPatient(caseNumber, isSign, currentPage, pageSize);
		return documents;
	}

	public int getSignDocumentNumByPatient(String caseNumber, byte isSign) {
		int number = documentDao.getSignDocumentNumByPatient(caseNumber,isSign);
		return number ;
	}

	public List<PatientDocument> getPatient(int currentPage, int pageSize) {
		return documentDao.getPatient(currentPage, pageSize);
	}
	public int getPatientNum() {
		return documentDao.getPatientNum();
	}

	public List<PatientDocument> getDocumentByCondition(PatientDocument patientDocument, int currentPage, int pageSize) {
		List<PatientDocument> documents = documentDao.getDocumentByCondition(patientDocument, currentPage, pageSize);
		return documents;
	}

	public int getDocumentNumByCondition(PatientDocument patientDocument) {
		return documentDao.getDocumentNumByCondition(patientDocument);
	}
}
